﻿package com.guavestudios.fnfo.plugins {
	import com.guavestudios.fnfo.events.FnfoPluginEvent;
	import com.guavestudios.fnfo.plugins.BasicFnfoPlugin;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author maze | ppmcity.com
	 */
	public class ButtonBarDisplay extends BasicFnfoPlugin{
		
		protected var buttonContainer:Sprite;
		protected var buttons:Array;
		
		public function ButtonBarDisplay() {
			super();
			buttons = new Array();
		}
		
		
		override protected function fnfo_initialize(e:Event):void {
			super.fnfo_initialize(e);
			
			buttonContainer = new Sprite();
			
			_fnfo.addChild(buttonContainer);
			
			//addSimpleTextButton("hallo","hallo");
			//addSimpleTextButton("du","du");
			//addSimpleTextButton("welt","welt");
		}
		
		override protected function fnfo_resize(e:Event):void {
			super.fnfo_resize(e);
			buttonContainer.x = _fnfo.getSize().width - buttonContainer.width-10;
			buttonContainer.y = _fnfo.getSize().height - buttonContainer.height-10;
		}
		override protected function fnfo_show(e:Event):void {
			super.fnfo_show(e);
			fnfo_resize(null);
		}
		
		public function addSimpleTextButton(caption:String,event:String):void {
			var btn:BasicButtonBarButton = new BasicButtonBarButton();
			btn.init(caption, event);
			addButton(btn);
			btn.addEventListener(MouseEvent.CLICK, btn_click);
		}
		
		private function btn_click(e:MouseEvent):void {
			var btn:BasicButtonBarButton = e.currentTarget as BasicButtonBarButton;
			_fnfo.dispatchEvent(new FnfoPluginEvent(btn.event, this._pluginId));
		}
		public function addButton(btn:BasicButtonBarButton):void {
			buttons.push(btn);
			buttonContainer.addChild(btn);
			
			layoutButtons();
		}
		
		private function layoutButtons():void {
			var btn:BasicButtonBarButton;
			var w:Number=0;
			for (var i:int = 0; i < buttons.length; i++) {
				btn = buttons[i];
				btn.x = w;
				w += btn.width+10;
			}
		}
	}

}